﻿using SyncFileCommon.BLL;
using SyncFileCommon.Class;
using SyncFileCommon.Model;
using System.ComponentModel;

namespace SyncFileOption
{
    public partial class FrmServerOption : Form
    {

        private BindingList<UserModel> UserList;

        private BindingList<SyncDirectoryModel> DirList;

        public FrmServerOption()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;//设置边框为不可调节
            this.MaximizeBox = false; //取消最大化按键
            this.MinimizeBox = false; //取消最小化按键
            this.ShowInTaskbar = false;//不在任务栏显示多个窗体框

        }

        private void FrmServerOption_Load(object sender, EventArgs e)
        {

            txtServerPort.Text = ConfigBLL.ServiceConfig.ServicePort.ToString();

            List<UserModel> _userList = UserBLL.UserListGet();
            if (_userList != null)
            {
                UserList = new BindingList<UserModel>(_userList);
            }
            else
            {
                UserList = new BindingList<UserModel>();
            }

            //设置禁止生成列
            gridUser.MultiSelect = false;
            gridUser.AutoGenerateColumns = false;
            gridUser.AllowUserToAddRows = false;
            gridUser.DataSource = UserList;

            List<SyncDirectoryModel> _dirList = SyncDirectoryBLL.DirectoryListGet();
            if (_dirList != null)
            {
                DirList = new BindingList<SyncDirectoryModel>(_dirList);
            }
            else
            {
                DirList = new BindingList<SyncDirectoryModel>();
            }

            //设置禁止生成列
            gridDir.MultiSelect = false;
            gridDir.AutoGenerateColumns = false;
            gridDir.AllowUserToAddRows = false;
            gridDir.DataSource = DirList;


        }

        private void BtnSystem_Click(object sender, EventArgs e)
        {
            pnlMain.AutoScrollPosition = new Point(0, -1 * pnlMain.AutoScrollPosition.Y + pnlSystem.Top);

            pnlMain.Refresh();
            pnlMain.Invalidate();
            pnlMain.Update();
        }

        private void btnUser_Click(object sender, EventArgs e)
        {
            pnlMain.AutoScrollPosition = new Point(0, -1 * pnlMain.AutoScrollPosition.Y + pnlUser.Top);

            pnlMain.Refresh();
            pnlMain.Invalidate();
            pnlMain.Update();
        }

        private void btnDir_Click(object sender, EventArgs e)
        {
            pnlMain.VerticalScroll.Value = -1 * pnlMain.AutoScrollPosition.Y + pnlDir.Top;

            pnlMain.Refresh();
            pnlMain.Invalidate();
            pnlMain.Update();

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //保存配置 
            ConfigBLL.ServiceConfig.ServicePort = Convert.ToInt32(txtServerPort.Text);

            List<UserModel> _userList = new List<UserModel>(UserList);
            UserBLL.UserListSave(_userList);

            List<SyncDirectoryModel> _dirList = new List<SyncDirectoryModel>(DirList);
            SyncDirectoryBLL.DirectoryListSave(_dirList);


            ConfigBLL.SaveConfig();

            this.DialogResult = DialogResult.OK;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnAddUser_Click(object sender, EventArgs e)
        {
            FrmUser _frmUser = new FrmUser();
            _frmUser.IsAdd = true;
            _frmUser.UserList = new List<UserModel>(UserList);
            DialogResult _dr = _frmUser.ShowDialog(this);

            if (_dr == DialogResult.OK)
            {
                if (UserList.FirstOrDefault(m => m.UserName == _frmUser.UserName) != null)
                {
                    MessageBox.Show("用户已存在,无法重复添加");
                    return;
                }
                UserModel _user = new UserModel();
                _user.UserName = _frmUser.UserName;
                _user.Password = _frmUser.Password;
                UserList.Add(_user);
            }
            else
            {

            }


        }


        private void gridUser_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // 确认点击的是按钮列
            if (e.ColumnIndex == gridUser.Columns["colUpdate"].Index && e.RowIndex >= 0)
            {
                // 执行按钮点击后的操作
                FrmUser _frmUser = new FrmUser();
                _frmUser.IsAdd = false;
                _frmUser.UserName = gridUser.Rows[e.RowIndex].Cells["colUserName"].Value.ToString();
                _frmUser.Password = gridUser.Rows[e.RowIndex].Cells["colPassword"].Value.ToString();
                DialogResult _dr = _frmUser.ShowDialog(this);

                if (_dr == DialogResult.OK)
                {
                    UserModel _user = UserList.FirstOrDefault(m => m.UserName == _frmUser.UserName);
                    _user.UserName = _frmUser.UserName;
                    _user.Password = _frmUser.Password;
                }
                else
                {

                }

            }

            if (e.ColumnIndex == gridUser.Columns["colDel"].Index && e.RowIndex >= 0)
            {
                // 执行按钮点击后的操作 
                string _userName = gridUser.Rows[e.RowIndex].Cells["colUserName"].Value.ToString();

                DialogResult result = MessageBox.Show(this, "确认删除用户[ " + _userName + " ]吗？", "提示",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (result == DialogResult.Yes)
                {
                    // 用户选择了"是"，执行删除操作    
                    UserList.Remove(UserList.FirstOrDefault(m => m.UserName == _userName));
                }
                else
                {
                    // 用户选择了"否"  
                }

            }
        }

        private void btnAddDir_Click(object sender, EventArgs e)
        {
            FrmDir _frmDir = new FrmDir();
            _frmDir.AllUserList = new List<UserModel>(UserList);
            _frmDir.DirList = new List<SyncDirectoryModel>(DirList);
            DialogResult _dr = _frmDir.ShowDialog(this);
            if (_dr == DialogResult.OK)
            {
                SyncDirectoryModel _dir = new SyncDirectoryModel();
                _dir.ID = CommonHelper.GetNewID();
                _dir.DirectoryPath = _frmDir.DirPath;
                _dir.UserList = _frmDir.UserList;
                DirList.Add(_dir);
            }
            else
            {

            }
        }

        private void gridDir_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // 确认点击的是按钮列
            if (e.ColumnIndex == gridDir.Columns["colDirUpdate"].Index && e.RowIndex >= 0)
            {
                // 执行按钮点击后的操作
                FrmDir _frmDir = new FrmDir();
                _frmDir.AllUserList = new List<UserModel>(UserList);
                _frmDir.IsAdd = false;
                _frmDir.ID = Convert.ToInt64(gridDir.Rows[e.RowIndex].Cells["colDirID"].Value.ToString());
                _frmDir.DirPath = gridDir.Rows[e.RowIndex].Cells["colDirPath"].Value.ToString();
                _frmDir.UserList = gridDir.Rows[e.RowIndex].Cells["colDirUserList"].Value.ToString();

                DialogResult _dr = _frmDir.ShowDialog(this);

                if (_dr == DialogResult.OK)
                {
                    SyncDirectoryModel _dir = DirList.FirstOrDefault(m => m.ID == _frmDir.ID);
                    
                    _dir.DirectoryPath = _frmDir.DirPath;
                    _dir.UserList = _frmDir.UserList;
                }
                else
                {

                }

            }

            if (e.ColumnIndex == gridDir.Columns["colDirDel"].Index && e.RowIndex >= 0)
            {
                // 执行按钮点击后的操作 1
                string _dirPath = gridDir.Rows[e.RowIndex].Cells["colDirPath"].Value.ToString();

                long _id = Convert.ToInt64(gridDir.Rows[e.RowIndex].Cells["colDirID"].Value.ToString());

                DialogResult result = MessageBox.Show("确认删除共享文件夹 [ " + _dirPath + " ]吗？", "提示",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (result == DialogResult.Yes)
                {
                    // 用户选择了"是"，执行删除操作    
                    DirList.Remove(DirList.FirstOrDefault(m => m.ID == _id));
                }
                else
                {
                    // 用户选择了"否"  
                }

            }
        }
    }
}
